package com.hd.sup.config.redis;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author chentl
 * @version V1.0
 * @Project 交通运输第三方监测平台
 * @Title RedisAutoConfig.java
 * @Description Redis装配类
 * @Package com.hd.sup.config.redis
 * @date 2020/12/24 10:35 上午
 * @Copyright: 深圳市宏电技术股份有限公司 All rights reserved.
 */
@Slf4j
@Configuration
@AutoConfigureBefore(RedisTemplate.class)
public class RedisConfig {

//    @Autowired(required = false)
//    private LettuceConnectionFactory lettuceConnectionFactory;
//
//
//    @Bean
//    public FastJson2JsonRedisSerializer fastJson2JsonRedisSerializer() {
//        return new FastJson2JsonRedisSerializer<>(Object.class);
//    }

    @Bean
    public JedisPoolConfig jedisPoolConfig(@Value("${spring.redis.jedis.pool.max-active}") int maxActive,
                                           @Value("${spring.redis.jedis.pool.max-idle}") int maxIdle,
                                           @Value("${spring.redis.jedis.pool.min-idle}") int minIdle,
                                           @Value("${spring.redis.jedis.pool.max-wait}") long maxWaitMillis,
                                           @Value("${spring.redis.block-when-exhausted}") boolean blockWhenExhausted) {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(maxActive);
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMinIdle(minIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        jedisPoolConfig.setBlockWhenExhausted(blockWhenExhausted);

        // 是否启用pool的jmx管理功能, 默认true
        jedisPoolConfig.setJmxEnabled(true);

        return jedisPoolConfig;
    }

    @Bean
    public JedisPool jedisPool(@Value("${spring.redis.host}") String host,
                               @Value("${spring.redis.password}") String password,
                               @Value("${spring.redis.port}") int port,
                               @Value("${spring.redis.timeout}") int timeout,
                               JedisPoolConfig jedisPoolConfig) {

        log.info("创建 JedisPool 连接池...");
        if (StringUtils.isNotEmpty(password)) {
            return new JedisPool(jedisPoolConfig, host, port, timeout, password);
        }

        return new JedisPool(jedisPoolConfig, host, port, timeout);
    }

//
//
//    /**
//     * 适配redis单节点
//     * RedisConnectionFactory fatory
//     *
//     * @return
//     */
//    @Primary
//    @Bean("redisTemplate")
//    @ConditionalOnProperty(name = "spring.redis.host", matchIfMissing = true)
//    public RedisTemplate<String, Object> getSingleRedisTemplate() {
//        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
//        redisTemplate.setConnectionFactory(lettuceConnectionFactory);
//        ObjectMapper objectMapper = new ObjectMapper();
//        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
//        FastJson2JsonRedisSerializer serializer = fastJson2JsonRedisSerializer();
//        serializer.setObjectMapper(objectMapper);
//
//        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//        // key采用String的序列化方式
//        redisTemplate.setKeySerializer(stringRedisSerializer);
//        // string的value采用fastJson序列化方式
//        redisTemplate.setValueSerializer(serializer);
//        // hash的key也采用String的序列化方式
//        redisTemplate.setHashKeySerializer(stringRedisSerializer);
//        // hash的value采用fastJson序列化方式
//        redisTemplate.setHashValueSerializer(serializer);
//        redisTemplate.afterPropertiesSet();
//
//        return redisTemplate;
//    }
//
//
//    @Bean
//    public HashOperations<String, String, String> hashOperations(StringRedisTemplate stringRedisTemplate) {
//        return stringRedisTemplate.opsForHash();
//    }

    /**
     * redis工具类
     //     */
//    @Bean("redisUtil")
//    public RedisUtil redisUtil(RedisTemplate<String, Object> redisTemplate, StringRedisTemplate stringRedisTemplate,
//                               HashOperations<String, String, String> hashOperations) {
//        RedisUtil redisUtil = new RedisUtil(redisTemplate, stringRedisTemplate, hashOperations);
//        return redisUtil;
//    }
}
